package com.deruan.hotcore.resource.gui.tree;

import java.awt.Component;
import java.awt.Font;

import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultTreeCellRenderer;

import org.columba.core.resourceloader.ImageLoader;
import org.columba.mail.resourceloader.MailImageLoader;

public class ResultTreeCellRenderer extends DefaultTreeCellRenderer
{
	private static Icon expandedFolderIcon;

	private static Icon collapsedFolderIcon;

	private static Icon virtualFolderIcon;

	private static Icon localRootFolderIcon;

	private static Icon imapRootFolderIcon;

	private static Icon inboxIcon;

	private static Icon outboxIcon;

	private static Icon trashIcon;

	private static Icon templateIcon;

	private static Icon sentIcon;

	static {
		collapsedFolderIcon = ImageLoader.getSmallIcon("folder.png");
		expandedFolderIcon = ImageLoader.getSmallIcon("folder-open.png");
		virtualFolderIcon = ImageLoader.getSmallIcon("folder-saved-search.png");
		localRootFolderIcon = ImageLoader.getSmallIcon("computer.png");
		imapRootFolderIcon = ImageLoader.getSmallIcon("network-server.png");
		inboxIcon = MailImageLoader.getSmallIcon("folder-inbox-new.png");
		outboxIcon = MailImageLoader.getSmallIcon("folder-outbox.png");

		templateIcon = MailImageLoader.getSmallIcon("folder-template.png");
		sentIcon = MailImageLoader.getSmallIcon("folder-sent.png");
		trashIcon = ImageLoader.getSmallIcon("user-trash.png");
	}

	private Font plainFont;

	private Font boldFont;

	private Font italicFont;
	
	/**
	 * Generates a new CellRenderer. In this contructor font and images are set
	 * to local variables. The fonts are depended on the current UIManager.
	 */
	public ResultTreeCellRenderer() {
		super();

		boldFont = UIManager.getFont("Tree.font");
		boldFont = boldFont.deriveFont(Font.BOLD);
		italicFont = UIManager.getFont("Tree.font");
		italicFont = italicFont.deriveFont(Font.ITALIC);
		plainFont = UIManager.getFont("Tree.font");
	}

	/**
	 * The tooltip text and unseen counter for the current folder component are
	 * set. If the folder has unseen Messages the folder self is show as bold
	 * and the unseen message counter is added to the folder label. The folder
	 * gets a tooltip where infos (unseen, recent, total) are set. If the folder
	 * is an IMAP folder and not selectable the folder is set to italic with a
	 * darkgrey background.
	 */
	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean isSelected, boolean expanded, boolean leaf, int row,
			boolean hasFocusVar) 
	{
		/*
		 * RIYAD: Even though we don't do anything with this value, what it is
		 * doing is setting up the selection colors and such as implemented per
		 * the default cell rendered.
		 */
		super.getTreeCellRendererComponent(tree, value, isSelected, expanded,
				leaf, row, hasFocusVar);
		if (value instanceof ResultTreeNode) 
		{
			ResultTreeNode node = (ResultTreeNode) value;
			setText(createLabelText(node));
			setIcon(getResultIcon(node, expanded));
			// set tooltip text
			//setToolTipText(createTooltipText(info));
		}
		return this;
	}

	/**
	 * Create HTML label text
	 *
	 * @param info
	 *            mailbox info (total/unread/recent count)
	 * @param item
	 *            folderitem containing xml-configuration
	 * @return label
	 */
	private String createLabelText(ResultTreeNode node) {

		// name of folder
		String name = node.getNameString();
		
		if(node instanceof ResourceTreeNode)
		{
			StringBuffer buf;
			buf = new StringBuffer("<html><body>");
			buf.append("<i><font color='#333333'>");
			buf.append("<b>");
			name = name + " (" + ((ResourceTreeNode)node).getDate().toString() + ")";
			buf.append(name);
			buf.append("</b>");
			buf.append("</i>");	
			buf.append("</body></html>");
			return buf.toString();
		}
		
		else if(node instanceof ContactTreeNode)
		{
			StringBuffer buf;
			buf = new StringBuffer("<html><body>");
			buf.append("<i><font color='#333333'>");
			buf.append("<b>");
			if(!((ContactTreeNode)node).isKnow())
			     name = "????";
			if(((ContactTreeNode)node).getResultNum()!=null)
			     name = name + " (" + ((ContactTreeNode)node).getResultNum() + ")";
			buf.append(name);
			buf.append("</b>");
			buf.append("</i>");		
			buf.append("</body></html>");		
			return buf.toString();
		}
		else if(node instanceof MessageSearchNode)
		{
			StringBuffer buf;
			buf = new StringBuffer("<html><body>");
			buf.append("<i><font color='#333333'>");
			buf.append("<b>");
			name = name + " (" + ((MessageSearchNode)node).getTitle() + ")";
			buf.append(name);
			buf.append("</b>");
			buf.append("</i>");		
			buf.append("</body></html>");		
			return buf.toString();
		}
		else if(node instanceof ResourceTypeNode||node instanceof ResultRootNode)
		{
			StringBuffer buf;
			buf = new StringBuffer("<html><body>");
			buf.append("<i><font color='#333333'>");
			buf.append("<b>");
			buf.append(name);
			buf.append("</b>");
			buf.append("</i>");		
			buf.append("</body></html>");		
			return buf.toString();
		}
        else 
        {
			return "";
		}
	}

	/**
	 * Create HTML tooltip text.
	 *
	 * @param info
	 *            mailboxinfo (total/unread/recent count)
	 * @return tooltip text
	 */
	private String createTooltipText(ResultTreeNode info) {
		StringBuffer buf = new StringBuffer();
		//buf.append("<html><body>&nbsp;Total: " + info.getExists());
		//buf.append("<br>&nbsp;Unseen: " + info.getUnseen());
		//buf.append("<br>&nbsp;Recent: " + info.getRecent());
		//buf.append("</body></html>");
		return buf.toString();
	}

	/**
	 * Returns an icon suitable for the given folder.
	 */
	public static Icon getResultIcon(ResultTreeNode node, boolean expanded) {
		if (node instanceof ResultRootNode) 
		{
			return localRootFolderIcon;
		} 
		else if (node instanceof ContactTreeNode) 
		{
			return imapRootFolderIcon;
		} 
		else if (node instanceof ResourceTreeNode) 
		{
			return virtualFolderIcon;
		}else if(node instanceof MessageSearchNode)
		{
			return ((MessageSearchNode)node).getStatusIcon();
		}
		
		return expanded ? expandedFolderIcon : collapsedFolderIcon;
	}

}
